package ru.svi.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PairedDevicesActivity extends Activity
{
	public static String EXTRA_PAIRED_DEVICES = "PairedDevice";

	private ArrayAdapter<Parcelable> pairedDevicesAdapter;
	private Parcelable[] pairedDevices;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.device_list);
		// Set result CANCELED incase the user backs out
		setResult(Activity.RESULT_CANCELED);

		pairedDevices = getIntent().getParcelableArrayExtra(EXTRA_PAIRED_DEVICES);	
		pairedDevicesAdapter = new ArrayAdapter<Parcelable>(this, R.layout.device_name,pairedDevices);

		// Find and set up the ListView for paired devices
		ListView pairedListView = (ListView) findViewById(R.id.devices);
		pairedListView.setAdapter(pairedDevicesAdapter);
		pairedListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				choose(position);
			}
		});

	}

	private void choose(int position) {
		Intent data = new Intent();
		data.putExtra(BlueToothThingActivity.EXTRA_DEVICE, pairedDevices[position]);
		setResult(Activity.RESULT_OK,data);
		finish();
	}
}
